package flc.ast.activity;

import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.appwidget.lib.AppWidgetAdder;

import java.util.Date;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.Constant;
import flc.ast.R;
import flc.ast.bean.ComponentBean;
import flc.ast.databinding.ActivityEditCalendarBinding;
import flc.ast.utils.LunarUtils;
import flc.ast.view.MyWidgetLong;
import flc.ast.view.MyWidgetShort;
import flc.ast.widget.calendar.Calendar10Widget;
import flc.ast.widget.calendar.Calendar1Widget;
import flc.ast.widget.calendar.Calendar2Widget;
import flc.ast.widget.calendar.Calendar3Widget;
import flc.ast.widget.calendar.Calendar4Widget;
import flc.ast.widget.calendar.Calendar5Widget;
import flc.ast.widget.calendar.Calendar6Widget;
import flc.ast.widget.calendar.Calendar7Widget;
import flc.ast.widget.calendar.Calendar8Widget;
import flc.ast.widget.calendar.Calendar9Widget;
import flc.ast.widget.time.Time10Widget;
import flc.ast.widget.time.Time1Widget;
import flc.ast.widget.time.Time2Widget;
import flc.ast.widget.time.Time3Widget;
import flc.ast.widget.time.Time4Widget;
import flc.ast.widget.time.Time5Widget;
import flc.ast.widget.time.Time6Widget;
import flc.ast.widget.time.Time7Widget;
import flc.ast.widget.time.Time8Widget;
import flc.ast.widget.time.Time9Widget;
import flc.ast.widget.weather.Weather1Widget;
import flc.ast.widget.weather.Weather2Widget;
import flc.ast.widget.weather.Weather3Widget;
import flc.ast.widget.weather.Weather4Widget;
import flc.ast.widget.weather.Weather5Widget;
import flc.ast.widget.weather.Weather6Widget;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.constant.Extra;
import stark.common.basic.device.BatteryUtil;
import stark.common.basic.utils.BitmapUtil;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;

public class EditCalendarActivity extends BaseAc<ActivityEditCalendarBinding> {

    private int mSelCalendar = 0;

    public static void start(Context context, int pos) {
        Intent intent = IntentUtil.getIntent(context, EditCalendarActivity.class);
        intent.putExtra(Extra.POS, pos);
        context.startActivity(intent);
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_edit_calendar;
    }

    @Override
    protected void initView() {
        mDataBinding.ivEditCalendarBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvEditCalendarAdd.setOnClickListener(this);

        mSelCalendar = getIntent().getIntExtra(Extra.POS, 0);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvEditCalendarAdd:
                addWidget();
                break;
            default:
                break;
        }
    }

    @Override
    protected void initData() {
        //显示小组件布局
        setLayout();
    }

    //添加小组件
    private void addWidget() {
        if (FastClickUtil.isFastClick()) {
            return;
        }
        switch (mSelCalendar) {
            case 1:
                createWidget(R.layout.widget_calendar1, MyWidgetShort.class);
                break;
            case 2:
                createWidget(R.layout.widget_calendar2, MyWidgetLong.class);
                break;
            case 3:
                createWidget(R.layout.widget_calendar3, MyWidgetShort.class);
                break;
            case 4:
                createWidget(R.layout.widget_calendar4, MyWidgetLong.class);
                break;
            case 5:
                createWidget(R.layout.widget_calendar5, MyWidgetLong.class);
                break;
            case 6:
                createWidget(R.layout.widget_calendar6, MyWidgetShort.class);
                break;
            case 7:
                createWidget(R.layout.widget_calendar7, MyWidgetLong.class);
                break;
            case 8:
                createWidget(R.layout.widget_calendar8, MyWidgetLong.class);
                break;
            case 9:
                createWidget(R.layout.widget_calendar9, MyWidgetLong.class);
                break;
            case 10:
                createWidget(R.layout.widget_calendar10, MyWidgetLong.class);
                break;
            default:
                break;
        }
    }

    //创建小组件
    private void createWidget(int layout, Class<? extends AppWidgetProvider> cls) {
        //要想应用能够接收到开机广播自启动，需具备以下两个条件，1、申请到悬浮窗权限 2、请求忽略电池优化(荣耀pro20手机实际测试到可以不用申请悬浮窗权限也能收到开机广播)
        //1、申请悬浮窗权限(如果权限请求被用户拒绝，是否每次进来都要请求请开发人员自己做处理，这里只是demo，直接每次都请求了)
        getPermission(layout, cls);
        //2、请求忽略电池优化(如果请求被用户拒绝，是否每次进来都要请求请开发人员自己做处理，这里只是demo，直接每次都请求了)
        BatteryUtil.reqIgnoreBatteryOptimizations(mContext);
    }

    //获取权限
    private void getPermission(int layout, Class<? extends AppWidgetProvider> cls) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            PermissionUtils.requestDrawOverlays(new PermissionUtils.SimpleCallback() {
                @Override
                public void onGranted() {
                    showDialog(getString(R.string.create_ing));
                    RxUtil.create(new RxUtil.Callback<Boolean>() {
                        private String filePath;

                        @Override
                        public void doBackground(ObservableEmitter<Boolean> emitter) {
                            Bitmap bitmap = null;
                            switch (mSelCalendar) {
                                case 1:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar1.llCalendar1);
                                    break;
                                case 2:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar2.llCalendar2);
                                    break;
                                case 3:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar3.llCalendar3);
                                    break;
                                case 4:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar4.llCalendar4);
                                    break;
                                case 5:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar5.llCalendar5);
                                    break;
                                case 6:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar6.llCalendar6);
                                    break;
                                case 7:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar7.llCalendar7);
                                    break;
                                case 8:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar8.llCalendar8);
                                    break;
                                case 9:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar9.llCalendar9);
                                    break;
                                case 10:
                                    bitmap = ImageUtils.view2Bitmap(mDataBinding.includeCalendar10.llCalendar10);
                                default:
                                    break;
                            }
//                            ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                            emitter.onNext(true);
                        }

                        @Override
                        public void accept(Boolean obj) {
                            dismissDialog();
                            AppWidgetAdder.getInstance().addAppWidget(layout, cls);
                        }
                    });
                }

                @Override
                public void onDenied() {
                    ToastUtils.showShort(R.string.permission_no_tips);
                }
            });
        }
    }

    //展示对应布局
    private void setLayout() {
        String date = TimeUtils.date2String(new Date(), "yyyy.MM.dd");
        String week = TimeUtils.date2String(new Date(), "E");
        String day = TimeUtils.date2String(new Date(), "dd");
        String date2 = TimeUtils.date2String(new Date(), "yyyy年MM月");
        LunarUtils lunarUtils = new LunarUtils(new Date());
        switch (mSelCalendar) {
            case 1:
                mDataBinding.includeCalendar1.tvCalendar1Date.setText(date);
                mDataBinding.includeCalendar1.tvCalendar1Week.setText(week);
                mDataBinding.includeCalendar1.llCalendar1.setVisibility(View.VISIBLE);
                break;
            case 2:
                mDataBinding.includeCalendar2.tvCalendar2Date.setText(date);
                mDataBinding.includeCalendar2.llCalendar2.setVisibility(View.VISIBLE);
                break;
            case 3:
                mDataBinding.includeCalendar3.ivCalendar3Date.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, date2, Color.WHITE, 15, "font/ziti6.ttf"));
                mDataBinding.includeCalendar3.ivCalendar3Day.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, day, Color.WHITE, 34, "font/ziti6.ttf"));
                mDataBinding.includeCalendar3.ivCalendar3Week.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, week, Color.WHITE, 12, "font/ziti6.ttf"));
                mDataBinding.includeCalendar3.ivCalendar3Month.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, lunarUtils.toString(), Color.WHITE, 12, "font/ziti6.ttf"));
                mDataBinding.includeCalendar3.llCalendar3.setVisibility(View.VISIBLE);
                break;
            case 4:
                mDataBinding.includeCalendar4.ivCalendar4Date.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, date2, Color.parseColor("#010101"), 15, "font/ziti7.ttf"));
                mDataBinding.includeCalendar4.ivCalendar4Day.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, day, Color.parseColor("#010101"), 34, "font/ziti7.ttf"));
                mDataBinding.includeCalendar4.llCalendar4.setVisibility(View.VISIBLE);
                break;
            case 5:
                mDataBinding.includeCalendar5.ivCalendar5Date.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, date2, Color.parseColor("#1D1D1D"), 12, "font/ziti7.ttf"));
                mDataBinding.includeCalendar5.ivCalendar5Day.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, day, Color.parseColor("#1D1D1D"), 42, "font/ziti7.ttf"));
                mDataBinding.includeCalendar5.tvCalendar5Week.setText(week);
                mDataBinding.includeCalendar5.tvCalendar5Month.setText(lunarUtils.toString());
                mDataBinding.includeCalendar5.llCalendar5.setVisibility(View.VISIBLE);
                break;
            case 6:
                mDataBinding.includeCalendar6.ivCalendar6Date.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, date2, Color.WHITE, 12, "font/ziti7.ttf"));
                mDataBinding.includeCalendar6.ivCalendar6Day.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, day, Color.WHITE, 42, "font/ziti7.ttf"));
                mDataBinding.includeCalendar6.ivCalendar6Week.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, week, Color.WHITE, 12, "font/ziti7.ttf"));
                mDataBinding.includeCalendar6.ivCalendar6Month.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, lunarUtils.toString(), Color.WHITE, 12, "font/ziti7.ttf"));
                mDataBinding.includeCalendar6.llCalendar6.setVisibility(View.VISIBLE);
                break;
            case 7:
                mDataBinding.includeCalendar7.ivCalendar7Date.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, date2, Color.parseColor("#361F1F"), 12, "font/ziti7.ttf"));
                mDataBinding.includeCalendar7.ivCalendar7Day.setImageBitmap(BitmapUtil.text2BmpWithAssetFont(mContext, day, Color.parseColor("#361F1F"), 42, "font/ziti7.ttf"));
                mDataBinding.includeCalendar7.tvCalendar7Week.setText(week);
                mDataBinding.includeCalendar7.tvCalendar7Month.setText(lunarUtils.toString());
                mDataBinding.includeCalendar7.llCalendar7.setVisibility(View.VISIBLE);
                break;
            case 8:
                mDataBinding.includeCalendar8.tvNum.setText(flc.ast.utils.BatteryUtil.level + "%");
                mDataBinding.includeCalendar8.llCalendar8.setVisibility(View.VISIBLE);
                break;
            case 9:
                mDataBinding.includeCalendar9.tvNum.setText(flc.ast.utils.BatteryUtil.level + "%");
                mDataBinding.includeCalendar9.llCalendar9.setVisibility(View.VISIBLE);
                break;
            case 10:
                mDataBinding.includeCalendar10.tvNum.setText(flc.ast.utils.BatteryUtil.level + "%");
                mDataBinding.includeCalendar10.llCalendar10.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }


}